<template>
  <view class="integral-container">
    <!-- 顶部导航 -->
    <view class="top-nav">
      <view class="nav-back" @click="goBack">
        <text class="icon">&#xf060;</text>
      </view>
      <text class="nav-title">我的积分</text>
      <view class="nav-placeholder"></view>
    </view>

    <!-- 积分余额卡片 -->
    <view class="balance-card">
      <view class="balance-content">
        <text class="balance-label">当前积分</text>
        <text class="balance-amount">{{
          userInfo.account_integral?.balance || 0
        }}</text>
        <view class="balance-info">
          <view class="info-item">
            <text class="info-label">累计获得</text>
            <text class="info-value">{{
              userInfo.account_integral?.total || 0
            }}</text>
          </view>
          <view class="info-divider"></view>
          <view class="info-item">
            <text class="info-label">累计消费</text>
            <text class="info-value">{{
              (userInfo.account_integral?.total || 0) -
              (userInfo.account_integral?.balance || 0)
            }}</text>
          </view>
        </view>
        <!-- 充值按钮 -->
        <view class="recharge-btn-wrapper">
          <button class="recharge-btn" @click="goToRecharge">
            <text class="btn-icon">&#xf09d;</text>
            <text>立即充值</text>
          </button>
          <view class="recharge-tip">
            <text class="icon">&#xf05a;</text>
            <text>1元 = 10积分</text>
          </view>
        </view>
      </view>
      <view class="balance-bg">
        <text class="bg-icon">💎</text>
      </view>
    </view>

    <!-- 筛选栏 -->
    <view class="filter-bar">
      <view class="filter-tabs">
        <view
          v-for="type in typeFilters"
          :key="type.value"
          class="filter-tab"
          :class="{ active: currentType === type.value }"
          @click="changeType(type.value)"
        >
          <text>{{ type.label }}</text>
        </view>
      </view>
      <view class="month-filter" @click="showMonthPicker = true">
        <text>{{ currentMonth || "选择月份" }}</text>
        <text class="icon">&#xf107;</text>
      </view>
    </view>

    <!-- 积分记录列表 -->
    <scroll-view
      scroll-y
      class="record-scroll"
      @scrolltolower="loadMore"
      :refresher-enabled="true"
      :refresher-triggered="refreshing"
      @refresherrefresh="onRefresh"
    >
      <view class="record-list">
        <view
          v-for="record in recordList"
          :key="record._id"
          class="record-item"
        >
          <view class="record-left">
            <view
              class="record-icon"
              :class="record.type === 1 ? 'income' : 'expense'"
            >
              <text class="icon">{{
                record.type === 1 ? "&#xf067;" : "&#xf068;"
              }}</text>
            </view>
            <view class="record-info">
              <text class="record-title">{{ record.title || "积分变动" }}</text>
              <text class="record-time">{{
                formatTime(record._add_time)
              }}</text>
            </view>
          </view>
          <view class="record-right">
            <text
              class="record-amount"
              :class="record.type === 1 ? 'income' : 'expense'"
            >
              {{ record.type === 1 ? "+" : "-" }}{{ record.value }}
            </text>
          </view>
        </view>

        <!-- 加载更多 -->
        <view v-if="hasMore" class="load-more">
          <text class="icon spinning">&#xf110;</text>
          <text>加载中...</text>
        </view>
        <view v-else-if="recordList.length > 0" class="no-more">
          <text>没有更多了</text>
        </view>

        <!-- 空状态 -->
        <view v-if="recordList.length === 0 && !loading" class="empty-state">
          <view class="empty-icon-wrapper">
            <text class="empty-icon">&#xf155;</text>
            <text class="empty-icon-bg">&#xf111;</text>
          </view>
          <text class="empty-text">暂无积分记录</text>
          <text class="empty-desc">快去完成任务赚取积分吧</text>
        </view>
      </view>
    </scroll-view>

    <!-- 月份选择器 -->
    <view
      v-if="showMonthPicker"
      class="picker-modal"
      @click="showMonthPicker = false"
    >
      <view class="picker-content" @click.stop>
        <view class="picker-header">
          <text class="picker-title">选择月份</text>
          <text class="picker-close" @click="showMonthPicker = false"
            >&#xf00d;</text
          >
        </view>
        <scroll-view scroll-y class="month-list">
          <view
            v-for="month in monthList"
            :key="month.value"
            class="month-item"
            :class="{ active: currentMonth === month.value }"
            @click="selectMonth(month.value)"
          >
            <text>{{ month.label }}</text>
            <text v-if="currentMonth === month.value" class="icon"
              >&#xf00c;</text
            >
          </view>
        </scroll-view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      userInfo: {},
      recordList: [],
      pageIndex: 1,
      pageSize: 20,
      hasMore: true,
      loading: false,
      refreshing: false,
      currentType: "全部",
      currentMonth: "",
      showMonthPicker: false,
      typeFilters: [
        { label: "全部", value: "全部" },
        { label: "收入", value: "Income" },
        { label: "支出", value: "Expense" },
      ],
      monthList: [],
    };
  },
  onLoad() {
    this.initMonthList();
    this.loadUserInfo();
    this.loadRecordList();
  },
  onShow() {
    this.loadUserInfo();
    this.loadRecordList();
  },
  methods: {
    // 加载用户信息
    async loadUserInfo() {
      try {
        const res = await vk.callFunction({
          url: "client/user.getMyInfo",
          data: {},
        });
        if (res.code === 0) {
          this.userInfo = res.userInfo;
        }
      } catch (error) {
        console.error("加载用户信息失败:", error);
      }
    },

    // 加载积分记录
    async loadRecordList(refresh = false) {
      if (this.loading) return;

      this.loading = true;
      if (refresh) {
        this.pageIndex = 1;
        this.recordList = [];
      }

      try {
        const res = await vk.callFunction({
          url: "client/user.getIntegralRec",
          data: {
            pageIndex: this.pageIndex,
            pageSize: this.pageSize,
            typeFilter: this.currentType,
            monthFilter: this.currentMonth,
          },
        });

        if (res.code === 0) {
          if (refresh) {
            this.recordList = res.rows || [];
          } else {
            this.recordList = this.recordList.concat(res.rows || []);
          }
          this.hasMore = res.hasMore || false;
        }
      } catch (error) {
        console.error("加载积分记录失败:", error);
        uni.showToast({
          title: "加载失败",
          icon: "none",
        });
      } finally {
        this.loading = false;
        this.refreshing = false;
      }
    },

    // 初始化月份列表（最近12个月）
    initMonthList() {
      const now = new Date();
      const list = [];

      for (let i = 0; i < 12; i++) {
        const date = new Date(now.getFullYear(), now.getMonth() - i, 1);
        const year = date.getFullYear();
        const month = (date.getMonth() + 1).toString().padStart(2, "0");
        const value = `${year}-${month}`;
        const label =
          i === 0 ? "本月" : i === 1 ? "上月" : `${year}年${month}月`;

        list.push({ value, label });
      }

      this.monthList = list;
    },

    // 切换类型筛选
    changeType(type) {
      this.currentType = type;
      this.loadRecordList(true);
    },

    // 选择月份
    selectMonth(month) {
      this.currentMonth = month;
      this.showMonthPicker = false;
      this.loadRecordList(true);
    },

    // 下拉刷新
    onRefresh() {
      this.refreshing = true;
      this.loadUserInfo();
      this.loadRecordList(true);
    },

    // 加载更多
    loadMore() {
      if (this.hasMore && !this.loading) {
        this.pageIndex++;
        this.loadRecordList();
      }
    },

    // 返回
    goBack() {
      uni.navigateBack();
    },

    // 跳转到充值页面
    goToRecharge() {
      uni.navigateTo({
        url: "/pages/user/wallet/wallet",
      });
    },

    // 格式化时间
    formatTime(timestamp) {
      const date = new Date(timestamp);
      const now = new Date();
      const diff = now - date;
      const minute = 60 * 1000;
      const hour = 60 * minute;
      const day = 24 * hour;

      if (diff < minute) {
        return "刚刚";
      } else if (diff < hour) {
        return Math.floor(diff / minute) + "分钟前";
      } else if (diff < day) {
        return Math.floor(diff / hour) + "小时前";
      } else {
        const Y = date.getFullYear();
        const M = (date.getMonth() + 1).toString().padStart(2, "0");
        const D = date.getDate().toString().padStart(2, "0");
        const h = date.getHours().toString().padStart(2, "0");
        const m = date.getMinutes().toString().padStart(2, "0");
        return `${M}-${D} ${h}:${m}`;
      }
    },
  },
};
</script>

<style scoped>
page {
  background: #0a0a0a;
}

.integral-container {
  min-height: 100vh;
  background: linear-gradient(180deg, #0a0a0a 0%, #1a1a2e 100%);
}

/* 顶部导航 */
.top-nav {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 30rpx 40rpx;
  padding-top: calc(30rpx + constant(safe-area-inset-top));
  padding-top: calc(30rpx + env(safe-area-inset-top));
  background: rgba(255, 255, 255, 0.03);
  backdrop-filter: blur(20px);
  border-bottom: 1rpx solid rgba(255, 255, 255, 0.05);
  z-index: 999;
}

.nav-back,
.nav-placeholder {
  width: 70rpx;
  height: 70rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

.nav-back {
  border-radius: 50%;
  background: rgba(139, 92, 246, 0.1);
  border: 1rpx solid rgba(139, 92, 246, 0.3);
}

.icon {
  font-family: FontAwesome;
  font-size: 32rpx;
  color: #8b5cf6;
}

.nav-title {
  font-size: 36rpx;
  font-weight: bold;
  color: #fff;
}

/* 积分余额卡片 */
.balance-card {
  position: relative;
  margin: calc(130rpx + constant(safe-area-inset-top)) 40rpx 30rpx;
  margin: calc(130rpx + env(safe-area-inset-top)) 40rpx 30rpx;
  padding: 50rpx 40rpx;
  background: linear-gradient(135deg, #8b5cf6 0%, #ec4899 100%);
  border-radius: 30rpx;
  overflow: hidden;
}

.balance-content {
  position: relative;
  z-index: 2;
}

.balance-label {
  display: block;
  font-size: 28rpx;
  color: rgba(255, 255, 255, 0.8);
  margin-bottom: 20rpx;
}

.balance-amount {
  display: block;
  font-size: 88rpx;
  font-weight: bold;
  color: #fff;
  margin-bottom: 30rpx;
}

.balance-info {
  display: flex;
  gap: 40rpx;
}

.info-item {
  display: flex;
  flex-direction: column;
  gap: 10rpx;
}

/* 充值按钮区域 */
.recharge-btn-wrapper {
  display: flex;
  align-items: center;
  gap: 20rpx;
  margin-top: 30rpx;
  padding-top: 30rpx;
  border-top: 1rpx solid rgba(255, 255, 255, 0.2);
}

.recharge-btn {
  flex: 1;
  height: 80rpx;
  background: rgba(255, 255, 255, 0.2);
  backdrop-filter: blur(10px);
  border: 1rpx solid rgba(255, 255, 255, 0.3);
  border-radius: 50rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12rpx;
  font-size: 28rpx;
  font-weight: bold;
  color: #fff;
  transition: all 0.3s;
  padding: 0;
}

.recharge-btn:active {
  background: rgba(255, 255, 255, 0.3);
  transform: scale(0.98);
}

.recharge-btn .btn-icon {
  font-family: FontAwesome;
  font-size: 28rpx;
}

.recharge-tip {
  display: flex;
  align-items: center;
  gap: 8rpx;
  padding: 12rpx 20rpx;
  background: rgba(255, 255, 255, 0.15);
  border-radius: 50rpx;
  font-size: 22rpx;
  color: rgba(255, 255, 255, 0.9);
  white-space: nowrap;
}

.recharge-tip .icon {
  font-size: 20rpx;
}

.info-label {
  font-size: 24rpx;
  color: rgba(255, 255, 255, 0.7);
}

.info-value {
  font-size: 32rpx;
  font-weight: bold;
  color: #fff;
}

.info-divider {
  width: 1rpx;
  background: rgba(255, 255, 255, 0.3);
}

.balance-bg {
  position: absolute;
  right: -40rpx;
  top: 50%;
  transform: translateY(-50%);
  opacity: 0.1;
  z-index: 1;
}

.bg-icon {
  font-size: 300rpx;
}

/* 筛选栏 */
.filter-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20rpx 40rpx;
  gap: 20rpx;
}

.filter-tabs {
  display: flex;
  gap: 20rpx;
  flex: 1;
}

.filter-tab {
  padding: 16rpx 32rpx;
  background: rgba(255, 255, 255, 0.03);
  border-radius: 50rpx;
  border: 1rpx solid rgba(255, 255, 255, 0.05);
  font-size: 28rpx;
  color: rgba(255, 255, 255, 0.6);
}

.filter-tab.active {
  background: rgba(139, 92, 246, 0.2);
  border-color: rgba(139, 92, 246, 0.5);
  color: #8b5cf6;
  font-weight: bold;
}

.month-filter {
  padding: 16rpx 32rpx;
  background: rgba(255, 255, 255, 0.03);
  border-radius: 50rpx;
  border: 1rpx solid rgba(255, 255, 255, 0.05);
  font-size: 28rpx;
  color: rgba(255, 255, 255, 0.6);
  display: flex;
  align-items: center;
  gap: 10rpx;
}

.month-filter .icon {
  font-size: 24rpx;
}

/* 记录列表 */
.record-scroll {
  height: calc(100vh - 550rpx - constant(safe-area-inset-top));
  height: calc(100vh - 550rpx - env(safe-area-inset-top));
}

.record-list {
  padding: 20rpx 40rpx 40rpx;
}

.record-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 30rpx;
  background: rgba(255, 255, 255, 0.03);
  border-radius: 20rpx;
  border: 1rpx solid rgba(255, 255, 255, 0.05);
  margin-bottom: 20rpx;
}

.record-left {
  display: flex;
  align-items: center;
  gap: 20rpx;
  flex: 1;
}

.record-icon {
  width: 80rpx;
  height: 80rpx;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.record-icon.income {
  background: rgba(34, 197, 94, 0.1);
}

.record-icon.income .icon {
  color: #22c55e;
}

.record-icon.expense {
  background: rgba(239, 68, 68, 0.1);
}

.record-icon.expense .icon {
  color: #ef4444;
}

.record-info {
  display: flex;
  flex-direction: column;
  gap: 8rpx;
}

.record-title {
  font-size: 30rpx;
  color: rgba(255, 255, 255, 0.9);
  font-weight: 500;
}

.record-time {
  font-size: 24rpx;
  color: rgba(255, 255, 255, 0.5);
}

.record-right {
  flex-shrink: 0;
}

.record-amount {
  font-size: 36rpx;
  font-weight: bold;
}

.record-amount.income {
  color: #22c55e;
}

.record-amount.expense {
  color: #ef4444;
}

/* 加载状态 */
.load-more,
.no-more {
  text-align: center;
  padding: 40rpx;
  font-size: 24rpx;
  color: rgba(255, 255, 255, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12rpx;
}

.load-more .icon {
  font-size: 32rpx;
}

/* 空状态 */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 120rpx 60rpx;
}

.empty-icon-wrapper {
  position: relative;
  width: 200rpx;
  height: 200rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 40rpx;
}

.empty-icon-bg {
  position: absolute;
  font-family: FontAwesome;
  font-size: 200rpx;
  color: rgba(139, 92, 246, 0.1);
  z-index: 1;
}

.empty-icon {
  position: relative;
  font-family: FontAwesome;
  font-size: 100rpx;
  background: linear-gradient(135deg, #8b5cf6 0%, #ec4899 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  z-index: 2;
}

.empty-text {
  font-size: 36rpx;
  font-weight: bold;
  color: rgba(255, 255, 255, 0.8);
  margin-bottom: 20rpx;
}

.empty-desc {
  font-size: 28rpx;
  color: rgba(255, 255, 255, 0.4);
}

/* 月份选择器 */
.picker-modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.8);
  display: flex;
  align-items: flex-end;
  z-index: 1000;
}

.picker-content {
  width: 100%;
  max-height: 70vh;
  background: #1a1a2e;
  border-radius: 40rpx 40rpx 0 0;
  padding: 40rpx;
}

.picker-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30rpx;
}

.picker-title {
  font-size: 36rpx;
  font-weight: bold;
  color: #fff;
}

.picker-close {
  font-family: FontAwesome;
  font-size: 40rpx;
  color: rgba(255, 255, 255, 0.6);
}

.month-list {
  max-height: 50vh;
}

.month-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 30rpx;
  background: rgba(255, 255, 255, 0.03);
  border-radius: 20rpx;
  margin-bottom: 20rpx;
  border: 1rpx solid rgba(255, 255, 255, 0.05);
  font-size: 30rpx;
  color: rgba(255, 255, 255, 0.8);
}

.month-item.active {
  background: rgba(139, 92, 246, 0.2);
  border-color: rgba(139, 92, 246, 0.5);
  color: #8b5cf6;
  font-weight: bold;
}

.month-item .icon {
  font-size: 28rpx;
  color: #8b5cf6;
}

/* 动画 */
@keyframes spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

.spinning {
  animation: spin 1s linear infinite;
}
</style>
